package com.homework;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class ReceiveA {
    public static void main(String[] args) throws IOException {
        //创建对象
        DatagramSocket datagramSocket=new DatagramSocket(7777);
        //接受数据
        byte [] buf=new byte[1024];
        DatagramPacket datagramPacket=new DatagramPacket(buf,buf.length);
        datagramSocket.receive(datagramPacket);

        //取数据
        byte []bytes=datagramPacket.getData();
        int len=datagramPacket.getLength();

        //读数据
        String anser=new String(bytes,0,len);
        if(anser.equals("四大名著是哪些?")){
            //返回数据
            byte[] book="红楼梦,西游记,三国演义,水浒传".getBytes();
            DatagramPacket datagramPacket2=new DatagramPacket(book,book.length, InetAddress.getLocalHost(),8848);
            //发送数据
            datagramSocket.send(datagramPacket2);
        }
        else{
            //返回数据
            byte[] book="what?".getBytes();
            DatagramPacket datagramPacket2=new DatagramPacket(book,book.length, InetAddress.getLocalHost(),8848);
            //发送数据
            datagramSocket.send(datagramPacket2);
        }









    }
}
